package lambda;

import org.junit.Test;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * Project: tobe-better-coder
 * Package: lambda
 * <p>
 * 1.Consumer<T> 消费型函数，接收一个参数T，没有返回值
 * 2.Supplier<T> 供给型接口，不接受参数，返回一个结果
 * 3.Function<T, R> 函数型接口，接受一个参数T，返回一个结果R
 * 4.predicate<T> 断定型接口，传一个参数，返回一个布尔值，其实就是函数型接口返回一个布尔值的简略写法，我的看法
 *
 * @author : Mr.xu
 * @date : 2023/9/18 23:08
 */
public class LambdaTest {

    @Test
    public void test1() {
        Consumer<String> consumer = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };

        consumer.accept("hello lambda");

        //
        Consumer<String> consumer2 = (s) -> System.out.println(s);
        consumer2.accept("hello lambda new");
    }

    @Test
    public void test2() {
        Supplier<String> stringSupplier = new Supplier<String>() {
            @Override
            public String get() {
                return String.valueOf(System.currentTimeMillis());
            }
        };
        System.out.println(stringSupplier.get());

        Supplier<String> stringSupplier2 = () -> String.valueOf(System.currentTimeMillis());
        System.out.println(stringSupplier2.get());
    }

    @Test
    public void test3() {
        Function<String, Boolean> function = new Function<String, Boolean>() {
            @Override
            public Boolean apply(String s) {
                return s.length() > 2;
            }
        };
        System.out.println(function.apply("111"));
        ;

        Function<String, Boolean> function2 = s -> s.length() > 2;


        System.out.println(function2.apply("333"));
        System.out.println(function2.apply("3"));
    }

    @Test
    public void test4() {
        Predicate<Integer> predicate = new Predicate<Integer>() {
            @Override
            public boolean test(Integer integer) {
                return integer % 2 == 0;
            }
        };

        System.out.println(predicate.test(2));

        Predicate<Integer> predicate2 = i -> i % 2 == 0;

        System.out.println(predicate2.test(2));

    }

    @Test
    public void test5() {
        LambdaTest lambdaTest = new LambdaTest();
//        Response<Boolean> result = lambdaTest.test6("123", lambdaTest::method);
//        System.out.println(result.getData());
//
//        Function<Integer,Boolean> function = LambdaTest :: testFunction;

        System.out.println(lambdaTest.test7(2, LambdaTest::testFunction));
    }

    public <T,R> R test7(T param ,Function<T,R> function){
        return function.apply(param);
    }

    public static Boolean testFunction(Integer i){
        return i%2 == 0;
    }

    public Boolean method(String s) {
        return s.length() > 2;
    }

    //这里consumer的accept方法是一个空实现
    public <T, R> Response<R> test6(T param, Function<T, R> function) {
        return this.test7(param, function, x -> {
            System.out.println("这里是个空实现");
        });
    }

    public <T, R> Response<R> test7(T param, Function<T, R> function, Consumer<T> consumer) {
        consumer.accept(param);
        return new Response<>(function.apply(param));
    }


}